<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<title>Insert title here</title>
	<link rel="stylesheet" href="${pageContext.request.contextPath}/homepages/css/main.css" type="text/css"></link>
    <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/js/themes/default/easyui.css" />
    <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/js/themes/icon.css" />
    <script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-1.4.4.min.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/js/locale/easyui-lang-zh_CN.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery.easyui.min.1.2.2.js"></script>
     <script type="text/javascript" src="${pageContext.request.contextPath}/homepages/js/forms.js"></script>
	<script type="text/javascript">
		var a = document.documentElement.clientHeight - 72;
		var b = document.documentElement.clientWidth - 25;
			
		var basePath = "/";
		$(function(){
			basePath = '${pageContext.request.contextPath}';
			$('#DATALIST').datagrid({
				iconCls:'icon-save',
				nowrap: false,
				width:b,
				height:a,
				striped: true,				 
				collapsible:true,
				autoRowHeight: false,		
				//sortName: 'code',
				//sortOrder: 'desc',
				remoteSort: false,
				//idField:'code',
				fitColumns:true,
				singleSelect:true,
				columns:[[]],
				pagination:true,
				rownumbers:true
				
			});
			 var formId= $('#formId').val();
			$.ajax({
			   url: "${pageContext.request.contextPath}/query/querymainlist",
			   type:"post",
			   data: {'formId':formId},
			   success: function(data){
			   		var columns = data.queryColumns;
			   		var buttons = data.queryButtons;
			   		var datagridPro = data.queryJson;
					var clnObj = $('#COLUMNS');
					var butObj = $('#BUTTONS');
					var clnHTML = "";
					var clineCount = 3;
					var rowCount = 0;
					for(var i = 0; i < columns.length; i++){
						if(rowCount%clineCount == 0)clnHTML += "<tr>";
						clnHTML += "<td>"+columns[i].colnName+"</td>";
						clnHTML += "<td>"+formatInputObj(columns[i])+"</td>";
						rowCount++;
						if(rowCount%clineCount == 0)clnHTML += "</tr>";
					}
					clnObj.html(clnHTML);
					var blineCount = 3;
					var butHTML = "";
					rowCount = 0;
					for(var i = 0; i < buttons.length; i++){
						butHTML += "<input type='button' value='"+buttons[i].butnName+"' onclick="+buttons[i].scriptFunc+" />";
					}
					butObj.html(butHTML);
					
					var dataGrid = $('#DATALIST');
					var options = {  
							rownumbers: true  
						};
					var columns = new Array();
					for(var i = 0; i < datagridPro.columns.length; i++){
						var column = {};
						column.title = data.queryJson.columns[i].colnName
						column.field = data.queryJson.columns[i].name;
						columns[i] = column;
					}
					var cc = new Array();
					cc[0] = columns;
					options.columns = (cc);
					dataGrid.datagrid(options);//根据配置选项，生成datagrid  
					//dataGrid.datagrid("loadData", data);
				}
			});
		});
		
		function resizeGrid(){
			var a = document.documentElement.clientHeight - 72;
			var b = document.documentElement.clientWidth - 25;
			$('#DATALIST').datagrid('resize', {
				width:b,
				height:a
 	  		 });
		}
		
				//弹出入单界面
		function addNew(dsId){
				$('#info').load('${pageContext.request.contextPath}/sys/inputForms',
					{'formType':dsId}); 
				alert($('#info').html());
				$('#info').show();
				$('#info').dialog({
					collapsible: true,
					minimizable: true,
					maximizable: true
					}); 
		}
		
		//循环所有的input框，找到那个框里有值的，进行拼接，最后传入后台
		function checkForm(){
			 var formId= $('#formId').val();
			 var conSql=" where 1=1 ";
			 var list = $('#COLUMNS input');
			 for(var i=0; i<list.length;i++){
			 	if(list[i].value!=""){
			 		conSql += " and "+list[i].name+" = '"+list[i].value+"'";
			 	}
			 }
			 $.ajax({
			 	type:'get',
			 	url:'../query/querySql',
			 	data:{'conditionSql':conSql,'formId':formId},
			 	success:function(data){
			 		var pk = data.tablePK;
					$('#DATALIST').datagrid("loadData", data);
					var dataPK ="";
					$('#DATALIST').datagrid({
						onDblClickRow:function(index,row){
							var srow = $('#DATALIST').datagrid('getSelected');
							var columns = $('#DATALIST').datagrid('getColumnFields');
							for(var i = 0;i < columns.length;i++){
								if(columns[i] == pk.trim()){
									alert(row[columns[i]]);
									dataPK=row[columns[i]];
									//showDialog(formId,dataPK);
								}
							}
						}
					})
			 	}
			 })
		}
	   
	   function formatInputObj(clnObj) {
			var inpHTML = "";
			var inpType = clnObj.inputType;
			var ediType = clnObj.ediType;
			var properName = "";
			if (clnObj.javaClass != null || clnObj.javaProperty != null
					|| clnObj.javaClass != "" || clnObj.javaProperty != "") {
				if (clnObj.javaClass != null && clnObj.javaProperty != null
						&& clnObj.javaClass != "" && clnObj.javaProperty != "") {
					properName = "name='" + clnObj.javaClass + "."
							+ clnObj.javaProperty + "'"
				}
				if ((clnObj.javaClass == null || clnObj.javaClass == "")
						&& (clnObj.javaProperty != null || clnObj.javaProperty != "")) {
					properName = "name='" + clnObj.javaProperty + "'"
				}
			}
			var openLoadSameStr = "";
			if (clnObj.openLoadSameStr == 'T' && clnObj.openLoadType != null
					&& clnObj.openLoadType != ""
					&& (inpType == 'input' || inpType == null || inpType == '')) {
				//查找相似的数据
				//openLoadSameStr = "  onkeydown= if(event.keyCode==13)loadSameStr(this,'"+clnObj.openLoadType+"','"+basePath+"');";
			}
			var width = clnObj.width;
			if (width == 0) {
				width = "width:100%;"
			} else {
				width = "width:" + width + "px;"
			}
			var height = clnObj.height;
			if (height == 0) {
				height = ""
			} else {
				height = "height:" + height + "px;"
			}
			var javaClass = clnObj.javaClass;
			var readOnly = clnObj.readOnly;
			var properties = properName + " " + openLoadSameStr;
			//对象类型 char integer number date time datetime input
			// datalist select textarea
			//radio checkbox [treelist] email
			//[link] password file [multivalue]
			switch (inpType) {
			case　"password":
				inpHTML ="<input " + properties + " type=\"password\" style='" + width+ height + "'/>";
				break;
			case　"file":
				inpHTML ="<input " + properties + " type=\"file\" style='" + width+ height + "'/>";
				break;
			case "radio":
				inpHTML ="<input " + properties + " type=\"radio\" style='" + width+ height + "'/>";
				break;
			case "checkbox":
				inpHTML ="<input " + properties + " type=\"checkbox\" style='" + width+ height + "'/>";
				break;
			case "email":   
				inpHTML ="<input " + properties + " id=\"email\" style='" + width+ height + "' onblur=\"checkNum(this)\"/>";
				break;
			case "select":
				inpHTML = "<select " + properties + " style='" + width + height 
						+ "' onClick=\"loadData()\"></select>";
				break;
			case "textarea":
				inpHTML = "<textarea " + properties +  "style='"+ width+ height+ "'></textarea>";
				break;
			case "datetime":
				inpHTML = "<input "+ properties+ " style='"+ width+ height+ "' onClick=\"WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss'})\"/>";
				break;
			case "integer":
				inpHTML = "<input " + properties + " id=\"intnum\" style='" + width
						+ height + "' onblur=\"checkNum(this)\"/>";
				break;
			case "number":
				inpHTML = "<input " + properties + " id=\"number\" style='" + width
						+ height + "' onblur=\"checkNum(this)\"/>";
				break;
			case "date":
				inpHTML = "<input " + properties + " style='" + width + height
						+ "' onClick=\"WdatePicker({dateFmt:'yyyy-MM-dd'})\"/>";
				break;
			case "time":
				inpHTML = "<input " + properties + " style='" + width + height
						+ "' onClick=\"WdatePicker({dateFmt:'HH:mm:ss'})\"/>";
				break;
			case "datalist":
				inpHTML = "<select " + properties + " style='" + width + height
						+ "' onClick=\"showDialog()\"></select>";
				break;
			default:
				inpHTML = "<input " + properties + " style='" + width + height
						+ "'/>";
			}
			return inpHTML;
		}
		
		function loadData(){
			alert("OK");
		}
		
		function showDialog(formId,dataPK){
			$('#dataInfo').show();
			$('#dataInfo').dialog({
				collapsible: true,
				minimizable: true,
				maximizable: true,
				toolbar: [{
					text: '添加',
				iconCls: 'icon-add',
				handler: function() {
					alert('添加数据')
					}
				},{
				text: '保存',
				iconCls: 'icon-save',
				handler: function() {
					alert('保存数据')
					}
				}] 
			}); 
		}
		
		//检查form表单中的数字框和email
		function checkNum(num) {
			var val = $(num).val();
			var id = $(num).attr('id');
			var re = "";
			if (id == "number") {
				re = /^[0-9]+.?[0-9]*$/;
				if (!re.test(val)) {
					alert("请输入数字(例:0.02或整数100)");
					return false;
				}
			} else if (id == "intnum") {
				re = /^[1-9]+[0-9]*]*$/;
				if (!re.test(val)) {
					alert("请输入正整数)");
					return false;
				}
			}else if(id=="email"){
				re=/^([a-zA-Z0-9]+[_|\-|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\-|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,4}$/;
				if (!re.test(val)) {
					alert("请输入E-mail的正确格式)");
					return false;
				}
			}else {
				return true;
			}
		}
		
	</script>
</head>
<body  onresize="resizeGrid();">
	<form action="" method="post" id="queryForm">
		<table cellpadding="0" cellspacing="0">
			<tr>
				<td>
					<!-- 部分查询框 -->
					<table id="COLUMNS">
					</table>
				</td>
				<td style="vertical-align: top;">
					<!-- 部分按钮 -->
					<div id="BUTTONS" style="width:195px;padding-left: 20px;"></div>
				</td>
			</tr>
			<tr>
				<td colspan="2">
					<!-- 数据列表 -->	
					<table  id="DATALIST"></table>
				</td>
			</tr>
		</table>
	</form>
	<div id="info" style="display:none;padding:5px;" title="新增${dsid}">
		<!-- 动态加载入单界面 -->
	</div>
	<div id="dataInfo" style="display:none;padding:5px;width:600px;height:300px;"></div>
	<input type="hidden" id="formId" value="${formId}"/>
</body>
</html>
